package com.xhwl.logistics.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.xhwl.common.dto.cent.alarm.AlarmManagementDTO;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.vo.cent.alarm.AlarmManagementVO;
import com.xhwl.logistics.dto.VehicleAlarmManagementPageDTO;
import com.xhwl.logistics.dto.VehicleAlarmStatisticDTO;
import com.xhwl.logistics.dto.manipulate.VehicleSyncDTO;
import com.xhwl.logistics.dto.query.VehicleAccessRecordSecondDTO;
import com.xhwl.logistics.service.IVehicleAlarmService;
import com.xhwl.logistics.service.cent.IAlarmManagementService;
import com.xhwl.logistics.util.AboutDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 停车场告警信息的具体服务层
 */
@Slf4j
//@Service
public class VehicleAlarmServiceImpl implements IVehicleAlarmService {

    @Autowired
    private IAlarmManagementService alarmManagementService;

    //停车场系统状态的告警码
    private static final String alarmVehicleSystemStatusCode= "39423849";
    //停车场系统的告警码
    private static final String alarmVehicleSystemCode="91912341";
    //当前页
    private static final Integer pageNum=1;
    //页大小
    private static final Integer pageSizeConstant=10;
    //时间
    private static final Integer time = 0;

    /**
     * 根据项目id查询停车场中的今日告警总览信息
     * @param vehicleSyncDTO
     * @return
     */
    @Override
    public VehicleAlarmStatisticDTO getAlarmStatistic(VehicleSyncDTO vehicleSyncDTO) {

        if(vehicleSyncDTO.getProjectId()==null){
            throw new BusinessException("缺少项目id参数");
        }
        AlarmManagementDTO alarmManagementDTO = toAlarmManagementDTO(vehicleSyncDTO.getProjectId(), pageSizeConstant,time);
        ResultJson resultJson = alarmManagementService.listPage(alarmManagementDTO);
        String resultStr = JSONObject.toJSONString(resultJson.getResult());
        VehicleAlarmManagementPageDTO vehicleAlarmManagementPageDTO1 = JSONObject.parseObject(resultStr, VehicleAlarmManagementPageDTO.class);
        AlarmManagementDTO alarmManagementDTO1 = toAlarmManagementDTO(vehicleSyncDTO.getProjectId(), vehicleAlarmManagementPageDTO1.getTotal(),time);
        ResultJson resultJson1 = alarmManagementService.listPage(alarmManagementDTO1);
        String resultStr1 = JSONObject.toJSONString(resultJson1.getResult());
        VehicleAlarmManagementPageDTO vehicleAlarmManagementPageDTO = JSONObject.parseObject(resultStr1, VehicleAlarmManagementPageDTO.class);
        List<AlarmManagementVO> records = vehicleAlarmManagementPageDTO.getRecords();
        List<AlarmManagementVO> alarmManagementVOList = records.stream().filter(s -> {
            if (s.getStatus() == 1 || s.getStatus() == 0) {
                return true;
            } else {
                return false;
            }
        }).collect(Collectors.toList());
        VehicleAlarmStatisticDTO vehicleAlarmStatisticDTO = new VehicleAlarmStatisticDTO();
        vehicleAlarmStatisticDTO.setAlarmTotal(vehicleAlarmManagementPageDTO.getTotal());
        vehicleAlarmStatisticDTO.setAlarmNoProcessed(alarmManagementVOList.size());
        vehicleAlarmStatisticDTO.setAlarmProcessed(vehicleAlarmManagementPageDTO.getTotal()-alarmManagementVOList.size());
        return vehicleAlarmStatisticDTO;
    }

    /**
     * 根据项目id获取停车场中的告警记录
     * @param vehicleAccessRecordSecondDTO
     * @return
     */
    @Override
    public List<AlarmManagementVO> getAlarmManagements(VehicleAccessRecordSecondDTO vehicleAccessRecordSecondDTO) {
        if(vehicleAccessRecordSecondDTO.getProjectId()==null){
            throw new BusinessException("缺少项目id参数");
        }
        AlarmManagementDTO alarmManagementDTO = toAlarmManagementDTO(vehicleAccessRecordSecondDTO.getProjectId(),
                vehicleAccessRecordSecondDTO.getPageSize(),1);
        ResultJson resultJson = alarmManagementService.listPage(alarmManagementDTO);
        VehicleAlarmManagementPageDTO vehicleAlarmManagementPageDTO = JSONObject.parseObject(JSONObject.toJSONString(resultJson.getResult()), VehicleAlarmManagementPageDTO.class);
        return vehicleAlarmManagementPageDTO.getRecords();
    }

    /**
     * 使用项目id构建AlarmManagementDTO，用于获取今日告警数
     */
    AlarmManagementDTO toAlarmManagementDTO(Integer projectId,Integer pageSize,Integer time){
        AlarmManagementDTO alarmManagementDTO = new AlarmManagementDTO();
        alarmManagementDTO.setProjectId(projectId);
        Date date = AboutDateUtil.theSameDayOfDate(new Date());
        //如果time为0，表示时间为当天
        if(StringUtils.equals(time.toString(),"0")){
            alarmManagementDTO.setStartTime(AboutDateUtil.dateToString(date));
            alarmManagementDTO.setEndTime(AboutDateUtil.dateToString(AboutDateUtil.datePlusDay(date,1)));
        }
        List<String> alarmSystemCodes = new ArrayList<>();
        alarmSystemCodes.add(alarmVehicleSystemStatusCode);
        alarmSystemCodes.add(alarmVehicleSystemCode);
        alarmManagementDTO.setAlarmSystemCodes(alarmSystemCodes);
        alarmManagementDTO.setPageNum(pageNum);
        alarmManagementDTO.setPageSize(pageSize);
        return alarmManagementDTO;
    }

}
